import { _decorator, Component, Node, Prefab, instantiate, UITransform, isValid } from 'cc';
import { GameSceneUI, GameState } from './GameSceneUI';
const { ccclass, property } = _decorator;

@ccclass('LevelManager')
export class LevelManager extends Component {
    private static instance: LevelManager = null;

    @property([Prefab]) 
    LevelNodeprefabList: Prefab[] = [];
    @property(Prefab)
    diNode: Prefab=null;
    @property(Prefab)
    guanNode: Prefab=null;


    diNodeImg:Node = null;
    guanNodeImg:Node = null;

    xOffset:number = 81;
    xOffset2:number = 65;

    numberNodeList:(Node | null)[]=new Array(10).fill(null);

    onLoad() {

        if (LevelManager.instance === null) 
        {
            LevelManager.instance = this;
        } else {
            console.error("LevelManager 已存在，请确保场景中只有一个 LevelManager");
            this.node.destroy();
        }
    }

    protected onDestroy(): void {
        for (let index = 0; index < this.numberNodeList.length; index++) {
            const element = this.numberNodeList[index];
            if(element && isValid(element))
            {
                this.node.removeChild(element);
                element.destroy();
            }
        }
        this.numberNodeList = [];
    }

    public reset() {
        // 重置单例类的状态
        LevelManager.instance = null;
    }

    public static getInstance(): LevelManager {
        return LevelManager.instance;
    }

    start() {
    }

    update(deltaTime: number) {
        
    }

    public updateLevel(nowLevel:number)
    {   
        if(GameSceneUI.getInstance().getGameState() != GameState.Init)
        {
            return;
        }

        if(this.diNodeImg == null)
        {
            this.diNodeImg = instantiate(this.diNode);
            let Transform = this.diNodeImg.getComponent(UITransform);
            Transform.width = 82;
            Transform.height = 71;
            this.node.addChild(this.diNodeImg);
        }
        this.diNodeImg.active = true;
        this.diNodeImg.setPosition(-120,0,0);

        const LevelStr = nowLevel.toString(); // 转换为字符串
        const LevelChars = Array.from(LevelStr, Number); // 分割为单个字符

        let lastPosx = 0;
        let row = 0
        for (; row < LevelChars.length; row++) 
        {
            // 获取图片节点
            const imageNode = this.LevelNodeprefabList[LevelChars[row]];
            if(imageNode == null)
            {
                console.error("imageNode1 IS NULL");
            }
            else
            {
                const ScoreImg = instantiate(imageNode);
                if(row <= 0)
                {
                    ScoreImg.setPosition(this.diNodeImg.position.x+(this.xOffset*(row+1)),0,0);
                }
                else
                {
                    ScoreImg.setPosition(lastPosx+this.xOffset2,0,0);
                }
                
                let Transform = ScoreImg.getComponent(UITransform);
                Transform.width = 45;
                Transform.height = 53;
                this.node.addChild(ScoreImg); 

                lastPosx = ScoreImg.position.x;
                this.numberNodeList[row] = ScoreImg;
            }
        }
        const finnallyNode = this.numberNodeList[row-1];
        if(finnallyNode)
        {
            if(this.guanNodeImg == null)
            {
                this.guanNodeImg = instantiate(this.guanNode);
                let Transform = this.guanNodeImg.getComponent(UITransform);
                Transform.width = 82;
                Transform.height = 71;
                this.node.addChild(this.guanNodeImg);
            }
            this.guanNodeImg.active = true;
            this.guanNodeImg.setPosition(finnallyNode.position.x+this.xOffset,0,0);
            
        }

    }

    public removeNode()
    {

            if(isValid(this.diNodeImg))
            {
                this.diNodeImg.active = false;
            }

            if(isValid(this.guanNodeImg))
            {
                this.guanNodeImg.active = false;
            }

            for (let index = 0; index < this.numberNodeList.length; index++) {
                const element = this.numberNodeList[index];
                if(isValid(element))
                {
                    element.active = false;
                    element.destroy();
                }
            }
            this.numberNodeList.fill(null);
    }
}


